//
//  Scoreboard.swift
//  AppleDemo
//
//  Created by Just 花语 on 2025/10/26.
//
import Foundation
struct Scoreboard{
    var players:[Player] = [
        Player(name: "Elisha", score: 0),
        Player(name: "Andre", score: 0),
        Player(name: "Jasmine", score: 0),
    ]
    var state = GameState.setup
    mutating func resetScores(to newScore:Int = 0){
        for i in players.indices{
            players[i].score = newScore
        }
    }
    
    var doesHighestScoreWin = true
    var winner:[Player]{
        guard state == .gameOver else {return []}
        var winningScore = 0
        if doesHighestScoreWin{
            winningScore = Int.min
            for player in players{
                winningScore = max(player.score,winningScore)
            }
        }else{
            winningScore = Int.max
            for player in players{
                winningScore = min(winningScore,player.score)
            }
        }
        return players.filter{ player in
            player.score == winningScore
        }
    }
}

struct Player:Identifiable{
    let id = UUID()
    var name:String
    var score:Int
}

extension Player:Equatable{
    static func == (l:Player,r:Player) ->Bool{
        return l.name == r.name && l.score == r.score
    }
}
